<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
      :clear-type="clearType"
    >
      <template v-slot:filter-supplierProdct>
        <slip-select
          v-model="filterInfo.query.queryParams.notDefaultProductSupplierId"
          value-key="value"
          placeholder="选择供货供应商"
          :api="supplierInfo.api"
          v-model:query="supplierInfo.query"
          key-field="supplierName"
          value-field="supplierId"
          :clearable="true"
          :filterable="true"
          :multiple="true"
          @change="search"
        />
      </template>
    </page-filter>
    <el-form ref="tableForm" :model="tableInfo" style="width: 100%">
      <dialog-table
        ref="resourceTable"
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :rowKey="rowKey"
        :highlight-row="highlightRow"
        :checkedRowList="checkedRowList"
        :reserve-selection="true"
        :check-box="checkBox"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-price="scope">
          <span v-if="scope.row.price || scope.row.price === 0">
            {{ (scope.row.price || 0) + '元/' + scope.row.networkUnit }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-qty="scope">
          {{ (scope.row.qty || 0) + scope.row.networkUnit }}
        </template>
      </dialog-table>
    </el-form>
  </page-dialog>
</template>
<script setup name="newSelectProduct">
import PageDialog from '@/components/PageDialog/index'
import DialogTable from '@/components/DialogTable/index'
import PageFilter from '@/components/NewPageFilter/index'
import SlipSelect from '@/components/SlipSelect/index'
import { getCustomizeSetting } from '@/api/customize'
import { getProductCustomizeList, getSupplierAll } from '@/api/core.js'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  api: {
    type: Function,
    default: getProductCustomizeList,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  showSupplierName: {
    type: Boolean,
    default: false,
  },
  notDefaultProductSupplier: {
    //是否供货供应商赛选
    type: Boolean,
    default: true,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  tableKey: {
    type: String,
    default:
      'provincePayOrderAddPendPayOrder-com.slip.spd.entity.vo.provincePlatform.ProvincePendPayOrderDetailVO',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  showEditQty: {
    type: Boolean,
    default: true,
  },
})

const systemConfig = ref(proxy.$store.state.permission.systemConfig)
const supplierInfo = ref({
  api: getSupplierAll,
  query: {
    supplierAuth: 'supplierId',
    sort: 'create_time desc',
  },
})
const clearType = ref(true)
// 过滤相关配置
const filterInfo = ref({
  copyQueryMap: {},
  query: {
    page: true,
    customizeUserVO: {},
    sortString: proxy.sortString,
    queryMap: {
      // type: {
      //   asTbName: 'core_product_new',
      //   columnName: 'type',
      //   queryResource: 0,
      //   queryType: 'EQUALS',
      //   queryCategory: 'AND',
      //   value: 0,
      // },
      status: {
        queryType: 'EQUALS',
        value: 0,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: _clearSearch },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
})
// 弹窗相关
const dialogInfo = ref({
  title: '新增支付明细',
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref([])

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

setData()
function setData() {
  let obj = {
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
    price: {
      label: '省采单价',
      value: 'price',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    qty: {
      label: '待结算数量',
      value: 'qty',
      type: 'slot',
      componentType: 'input',
      hidden: true,
    },
    statusValue: {
      label: '状态',
      value: 'statusValue',
      asValue: 'status',
      componentType: 'select-model',
      code: 'province_pend_pay_status',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function _clearSearch() {
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      if (!props.showEditQty) {
        tableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'editQty') {
            item.hidden = true
          }
        })
      }
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      let supplierProdct = {
        slot: true,
        value: 'supplierProdct',
      }
      if (props.notDefaultProductSupplier) {
        filterInfo.value.searchList =
          searchInfo.searchList.concat(supplierProdct)
      } else {
        filterInfo.value.searchList = searchInfo.searchList
      }
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
        if (props.query.supplierId.value) {
          if (item.value === 'supplierName') {
            item.show = false
          }
        } else {
          if (item.value === 'supplierName') {
            item.show = true
          }
        }
      })
      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...props.query,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.query.queryParams = {
        ...filterInfo.value.query.queryParams,
        ...props.queryParams,
      }
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      tableInfo.value.api = props.api
      console.log(tableInfo.value.fieldList)
      getList()
    }
  })
}
</script>

<style scoped lang="scss"></style>
